/**
 * 
 */
package com.ssm.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ssm.exception.SsmException;
import com.ssm.exception.WebException;
import com.ssm.model.Model;
import com.ssm.model.bean.SsmWebSetting;
import com.ssm.model.mapper.WebSettingMapper;

/**
 * 获取网站参数设置工具类
 * @author 危文涛<245108903@qq.com>
 * @version 2016年2月24日 下午1:13:39
 *
 */
@Component
public class WebSettingUtil {

	@Autowired(required=true)
	private Model M;
	public Object getWebSetting(String settingName)
	{
		SsmWebSetting webSetting=(SsmWebSetting) EhcacheUtil.get("WebSettingValues",settingName);
		if(webSetting!=null)
			return webSetting.getValue();
		WebSettingMapper webSettingMapper=M.getMapper(WebSettingMapper.class);
		webSetting=new SsmWebSetting();
		webSetting.setName(settingName);
		try{
		webSetting=webSettingMapper.selectOne(webSetting);
		EhcacheUtil.put("WebSettingValues",settingName,webSetting);
		}
		catch(Exception e)
		{
			throw new WebException("出现多个数据",e);
		}
		if(webSetting==null||webSetting.getValue()==null||webSetting.getValue()=="")
		{
			throw new WebException("name:"+settingName+" 没有对应的网站设置");
		}
		return webSetting.getValue();
	}
		
	public String getString(String settingName)
	{
		return (String)getWebSetting(settingName);
	}
	
	public Integer getInteger(String settingName)
	{
		return Integer.parseInt((String) getWebSetting(settingName));
	}
	
	public Boolean getBoolean(String settingName)
	{
		return Boolean.valueOf((String) getWebSetting(settingName));
	}
	
	
	
}
